package com.ywjh.inter.service;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.ywjh.inter.common.SystemConfig;
import com.ywjh.inter.conn.DBConnection;
import com.ywjh.inter.dao.HXFriendDAO;
import com.ywjh.inter.dao.UsersHXDAO;
import com.ywjh.inter.entity.FriendHXEntity;
import com.ywjh.inter.entity.UsersHXEntity;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class ChangeServer {

	private static UsersHXDAO usersHXDAO = new UsersHXDAO();
	private static HXFriendDAO hxFriendDAO = new HXFriendDAO();

	public static void main(String[] args) throws Exception {
		// get();
		// set();
		//test();
		check("user_4293");
	}

	private static void check(String string) throws IOException {
		// TODO Auto-generated method stub
		JSONObject json = HXService.getInstance().getToken(SystemConfig.CLIENT_ID, SystemConfig.CLIENT_SECERT);
		String token = json.getString("access_token");
		json = HXService.getInstance().getFriendList(string, token);
		List<String> friendList = (List<String>) JSONArray.toCollection(json.getJSONArray("data"), String.class);
	}

	private static void test() throws IOException, ClassNotFoundException, SQLException {
		Connection connection = DBConnection.getDBConnection();
		List<UsersHXEntity> list = usersHXDAO.list(connection);
		JSONObject json = HXService.getInstance().getToken(SystemConfig.CLIENT_ID, SystemConfig.CLIENT_SECERT);
		String token = json.getString("access_token");
		for (UsersHXEntity hxEntity : list) {
			// TODO Auto-generated method stub
			json = HXService.getInstance().getFriendList(hxEntity.getUsername(), token);
			List<String> friendList = (List<String>) JSONArray.toCollection(json.getJSONArray("data"), String.class);
			int hxCount=friendList.size();
			
			int localCount=hxFriendDAO.countFriend(connection,hxEntity.getUsername());
			if(hxCount!=localCount) {
				System.out.println("环信数据："+hxCount);
				System.out.println("本地数据："+localCount);
				System.out.println("username："+hxEntity.getUsername());
				System.out.println("---------------------");
			}
			
		}
		DBConnection.close(connection);
	}

	private static void set() throws Exception, SQLException {
		Connection connection = DBConnection.getDBConnection();
		List<FriendHXEntity> list = hxFriendDAO.list(connection);
		JSONObject json = HXService.getInstance().getToken(SystemConfig.CLIENT_ID, SystemConfig.CLIENT_SECERT);
		String oldServerToken = json.getString("access_token");
		for (FriendHXEntity entity : list) {
			String a = entity.getUsername();
			String b = entity.getFriend_name();
			System.out.println("a=" + a + " b=" + b);
			try {
				HXService.getInstance().addFriend(a, b, oldServerToken);
				entity.setStatus(0);
				hxFriendDAO.update(connection, entity);
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}

		}
		DBConnection.close(connection);
	}

	private static void get() throws Exception {
		Connection connection = DBConnection.getDBConnection();
		List<UsersHXEntity> list = usersHXDAO.list(connection);
		JSONObject json = HXService.getInstance().getToken(SystemConfig.CLIENT_ID, SystemConfig.CLIENT_SECERT);
		String oldServerToken = json.getString("access_token");
		// 读取好友列表 写入数据库
		for (UsersHXEntity entity : list) {
			String hxname = entity.getUsername();
			// writeFriend(hxname, oldServerToken, connection);
			writeBlack(hxname, oldServerToken, connection);
		}
		connection.close();
	}

	private static void writeFriend(String hxname, String token, Connection connection)
			throws IOException, SQLException {
		JSONObject json = HXService.getInstance().getFriendList(hxname, token);
		List<String> friendList = (List<String>) JSONArray.toCollection(json.getJSONArray("data"), String.class);
		System.out.println(friendList.size());
		hxFriendDAO.add(connection, hxname, friendList);
	}

	private static void writeBlack(String hxname, String token, Connection connection) throws IOException {
		JSONObject json = HXService.getInstance().getBlacklist(hxname, token);
		List<String> friendList = (List<String>) JSONArray.toCollection(json.getJSONArray("data"), String.class);
		if (friendList.size() > 0) {
			System.out.println(friendList.size());
		}
	}

}
